home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_100 / 130_01 / rytest.c < prev    next >
Text File  |  1985-03-09  |  5KB  |  288 lines

  1. /*** - the random file code (ry) tester */
  2.  
  3. #include "a:std.h"        /* bdscio + my stuff */
  4. #include <ry.h>
  5.  
  6. /*
  7.     the exerciser....
  8. */
  9.  
  10. char Name[15];
  11. char Open;
  12. char Pos[4], Apos[15];
  13. int Org;
  14. RFILE *Fp;
  15.  
  16. main()
  17. {
  18.                                                         /* print menu */
  19. _allocp = NULL;
  20.  
  21. Name[0] = 0;
  22. Open = NO;
  23.  
  24. top:
  25.     printf("\f\nwhat to do?\n");
  26.     printf("\n1: creat a random file");
  27.     printf("\n2: open a random file");
  28.     printf("\n3: seek a position");
  29.     printf("\n4: read present position");
  30.     printf("\n5: get a character");
  31.     printf("\n6: put a character");
  32.     printf("\n7: get a line");
  33.     printf("\n8: put a line");
  34.     printf("\n9: close a random file");
  35.  
  36.     printf("\n\n    - ");
  37. /** go for it */
  38.     switch(getchar()) {            /* get choice */
  39.     case '1':
  40.         makerf();
  41.         break;
  42.     case '2':
  43.         openrf();
  44.         break;
  45.     case '3':
  46.         seekp();
  47.         break;
  48.     case '4':
  49.         readp();
  50.         break;
  51.     case '5':
  52.         gc();
  53.         break;
  54.     case '6':
  55.         pc();
  56.         break;
  57.     case '7':
  58.         gl();
  59.         break;
  60.     case '8':
  61.         pl();
  62.         break;
  63.     case '9':
  64.         closerf();
  65.         break;
  66.     default:
  67.         printf("\n\t\thuh?");
  68.     }
  69.     printf("\nagain? ");
  70.     if (tolower(getchar()) == 'n') exit();
  71.     goto top;
  72. }
  73.  
  74. makerf()
  75. {
  76.     char mode;
  77.     int sectors;
  78.  
  79.     if (Open) {
  80.         printf("\nfile (%s) already open!", Name);
  81.         return ERROR;
  82.     }
  83.     printf("\nfile name? ");
  84.     scanf("%s", Name);
  85.     printf("\nmode (r, w, d) ? ");
  86.     mode = tolower(getchar());
  87.     printf("\nbuffer size in sectors ? ");
  88.     scanf("%d", §ors);
  89.     if ((Fp = rcreat(Name, mode, sectors)) == ERROR) {
  90.         printf("\n ERROR from rfmake!!!");
  91.         return ERROR;
  92.     }
  93.     Open = YES;
  94.     return OK;
  95. }
  96.  
  97. openrf()
  98. {
  99.     char mode;
  100.     int sectors;
  101.  
  102.     if (Open) {
  103.         printf("\nfile (%s) already open!", Name);
  104.         return ERROR;
  105.     }
  106.     printf("\nfile name? ");
  107.     scanf("%s", Name);
  108.     printf("\nmode (r, w, d) ? ");
  109.     mode = tolower(getchar());
  110.     printf("\nbuffer size in sectors ? ");
  111.     scanf("%d", §ors);
  112.     if ((Fp = ropen(Name, mode, sectors)) == ERROR) {
  113.         printf("\n ERROR from rfopen!!!");
  114.         return ERROR;
  115.     }
  116.     Open = YES;
  117.     return OK;
  118. }
  119.  
  120. closerf()
  121. {
  122.     if (rclose(Fp) == ERROR) {
  123.         printf("\nERROR from rfclose (%s) !!!, Name");
  124.         Open = NO;
  125.         Name[0] = 0;
  126.         return ERROR;
  127.     }
  128.     Open = NO;
  129.     Name[0] = 0;
  130.     return OK;
  131. }
  132.  
  133. seekp()
  134. {
  135.     printf("\nwhat position do u want ? ");
  136.     scanf("%s", Apos);
  137.     atol(Pos, Apos);
  138.     printf("\nfrom 0:beginning, 1:present pos, or 2:end  (internal form)");
  139.     printf("\nfrom 3:beginning, 4:present pos, or 5:end  (ascii form)");
  140.     printf("\n\t- ");
  141.     scanf("%d", &Org);
  142.     switch (Org) {
  143.     case 0:
  144.     case 1:
  145.         if (lseek(Fp, Pos, Org) == ERROR) {
  146.             printf("\nERROR on lseek (internal form)!");
  147.             return ERROR;
  148.         }
  149.         return OK;
  150.     case 3:
  151.     case 4:
  152.         if (lseek(Fp, Apos, Org) == ERROR) {
  153.             printf("\nERROR on lseek (ascii form)!");
  154.             return ERROR;
  155.         }
  156.         return OK;
  157.     case 2:
  158.     case 5:
  159.         printf("\nseeks from end not supported yet...");
  160.         return ERROR;
  161.     default:
  162.         printf("\nbad origin");
  163.         return ERROR;
  164.     }
  165. }
  166.  
  167. readp()
  168. {
  169.     printf("\ncurrent file position: %s", ltoa(Apos, ltell(Fp, Pos)));
  170.     return OK;
  171. }
  172.  
  173. gc()
  174. {
  175.     int c;
  176.  
  177.     ltoa(Apos, ltell(Fp, Pos));
  178.     if ((c = rgetc(Fp)) == ERROR) {
  179.         printf("\nERROR on rgetc from pos %s", Apos);
  180.         return ERROR;
  181.     }
  182.     printf("\ngot '%c' from position %s", c, Apos);
  183.     return OK;
  184. }
  185.  
  186. pc()
  187. {
  188.     char c;
  189.  
  190.     ltoa(Apos, ltell(Fp, Pos));
  191.     printf("\nwhat character ? ");
  192.     scanf("%c", &c);
  193.     if (rputc(c, Fp) == ERROR) {
  194.         printf("\nERROR on rputc at pos %s", Apos);
  195.         return ERROR;
  196.     }
  197.     printf("\nput '%c' at position %s", c, Apos);
  198.     return OK;
  199. }
  200.  
  201. gs()
  202. {
  203.     char str[MAXLINE];
  204.  
  205.     ltoa(Apos, ltell(Fp, Pos));
  206.     if (rgets(str, Fp) == ERROR) {
  207.         printf("\nERROR on rgets from pos %s", Apos);
  208.         return ERROR;
  209.     }
  210.     printf("\ngot string '%s' from position %s", str, Apos);
  211.     return OK;
  212. }
  213.  
  214. ps()
  215. {
  216.     char str[MAXLINE];
  217.  
  218.     ltoa(Apos, ltell(Fp, Pos));
  219.     printf("\nwhat string ? ");
  220.     scanf("%s", str);
  221.     if (rputs(str, Fp) == ERROR) {
  222.         printf("\nERROR on rputs at pos %s", Apos);
  223.         return ERROR;
  224.     }
  225.     printf("\put string '%s' at position %s", str, Apos);
  226.     return OK;
  227. }
  228.  
  229. gl()
  230. {
  231.     char str[MAXLINE];
  232.  
  233.     ltoa(Apos, ltell(Fp, Pos));
  234.     if (rgetl(str, Fp) == ERROR) {
  235.         printf("\nERROR on rgetl from pos %s", Apos);
  236.         return ERROR;
  237.     }
  238.     printf("\ngot line '%s' from position %s", str, Apos);
  239.     return OK;
  240. }
  241.  
  242. pl()
  243. {
  244.     char str[MAXLINE];
  245.  
  246.     ltoa(Apos, ltell(Fp, Pos));
  247.     printf("\nwhat line ? ");
  248.     scanf("%s", str);
  249.     strcat(str, "\n");
  250.     if (rputl(str, Fp) == ERROR) {
  251.         printf("\nERROR on rputl at pos %s", Apos);
  252.         return ERROR;
  253.     }
  254.     printf("\put line '%s' at position %s", str, Apos);
  255.     return OK;
  256. }
  257.  
  258. /***
  259. g()
  260. {
  261.     char block[MAXLINE];
  262.  
  263.     ltoa(Apos, ltell(Fp, Pos));
  264.     if (rget(Fp, block, num) == ERROR) {
  265.         printf("\nERROR on rget from pos %s", Apos);
  266.         return ERROR;
  267.     }
  268.     printf("\ngot block '%s' from position %s", str, Apos);
  269.     return OK;
  270. }
  271.  
  272. p()
  273. {
  274.     char str[MAXLINE];
  275.  
  276.     ltoa(Apos, ltell(Fp, Pos));
  277.     printf("\nwhat string ? ");
  278.     scanf("%s", str);
  279.     if (rputs(str, Fp) == ERROR) {
  280.         printf("\nERROR on rputs at pos %s", Apos);
  281.         return ERROR;
  282.     }
  283.     printf("\put string '%s' at position %s", str, Apos);
  284.     return OK;
  285. }
  286. ***/
  287. f (rgetl(str, Fp) == ERROR) {
  288.         printf("\nERROR on rgetl from pos %s"